package in.srain.cube.views.ptr;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class PtrClassicFrameLayout extends PtrFrameLayout {

    private PtrClassicCustomHeader mPtrClassicHeader;
    private PtrClassicCustomFooter mPtrClassicFooter;
    public PtrClassicFrameLayout(Context context) {
        super(context);
        initViews();
    }

    public PtrClassicFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public PtrClassicFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViews();
    }
    private void initViews() {

            mPtrClassicHeader = new PtrClassicCustomHeader(getContext());
            setHeaderView(mPtrClassicHeader);
            addPtrUIHandler(mPtrClassicHeader);

            mPtrClassicFooter = new PtrClassicCustomFooter(getContext());
            setFooterView(mPtrClassicFooter);
            addPtrUIHandler(mPtrClassicFooter);


    }

    public PtrClassicCustomHeader getHeader() {
        return mPtrClassicHeader;
    }

    /**
     * Specify the last update time by this key string
     *
     * @param key
     */
    public void setLastUpdateTimeKey(String key) {
        setLastUpdateTimeHeaderKey(key);
        setLastUpdateTimeFooterKey(key);
    }

    public void setLastUpdateTimeHeaderKey(String key) {
        if (mPtrClassicHeader != null) {
            mPtrClassicHeader.setLastUpdateTimeKey(key);
        }
    }

    public void setLastUpdateTimeFooterKey(String key) {
        if (mPtrClassicFooter != null) {
            mPtrClassicFooter.setLastUpdateTimeKey(key);
        }
    }

    /**
     * Using an object to specify the last update time.
     *
     * @param object
     */
    public void setLastUpdateTimeRelateObject(Object object) {
        setLastUpdateTimeHeaderRelateObject(object);
        setLastUpdateTimeFooterRelateObject(object);
    }

    public void setLastUpdateTimeHeaderRelateObject(Object object) {
        if (mPtrClassicHeader != null) {
            mPtrClassicHeader.setLastUpdateTimeRelateObject(object);
        }
    }

    public void setLastUpdateTimeFooterRelateObject(Object object) {
        if (mPtrClassicFooter != null) {
            mPtrClassicFooter.setLastUpdateTimeRelateObject(object);
        }
    }
    public void setFootVisible(boolean visible){
        if (mPtrClassicFooter!=null){
            if (visible){
                mPtrClassicFooter.setVisibility(View.VISIBLE);
                setKeepHeaderWhenRefresh(true);
            }else {
                mPtrClassicFooter.setVisibility(View.GONE);
               setKeepHeaderWhenRefresh(false);
            }

        }
    }
}
